<template>
  <div class="rank" ref="rank">
    <scroll class="toplist" :data="topList" ref="toplist">
      <ul>
        <li class="item" v-for="item in topList" @click.stop="selectListItem(item)">
          <div class="icon">
            <img width="100" height="100" v-lazy="item.picUrl">
          </div>
          <ul class="songlist">
            <li class="song" v-for="(song,index) in item.songList">
              <span>{{index + 1}}</span>
              <span>{{song.songname}}-{{song.singername}}</span>
            </li>
          </ul>
        </li>
      </ul>
    </scroll>
    <div class="loading-wrap">
      <loading v-show="topList.length < 1"></loading>
    </div>
    <router-view></router-view>
  </div>
</template>

<script>
  import {playlistMixin} from 'common/js/mixin.js';
  import {getTopList} from 'api/rank.js';
  import {ERR_OK} from 'api/config.js';
  import Scroll from '../base/scroll/scroll.vue';
  import Loading from '../base/loading/loading.vue';
  import {mapMutations} from 'vuex';

  export default {
    mixins: [playlistMixin],
    components: {Scroll, Loading},
    data () {
      return {
        topList: []
      };
    },
    methods: {
      selectListItem (item) {
        this.$router.push({
          path: `/rank/${item.id}`
        });
        this.setTopList(item);
      },
      handlePlaylistAction (list) {
        const bottom = list.length > 0 ? '60px' : 0;
        this.$refs.rank.style.bottom = bottom;
        this.$refs.toplist.refresh();
      },
      _getTopList () {
        getTopList().then(res => {
          if (res.code === ERR_OK) {
            this.topList = res.data.topList;
          } else {
            console.log(`get toplist err:code=${res.code}`);
          }
        }).catch(() => {
          console.log(`get toplist err`);
        });
      },
      ...mapMutations({
        'setTopList': 'SET_TOP_LIST'
      })
    },
    created () {
      this._getTopList();
    }
  };
</script>

<style lang="less" scoped="">
  @import "~common/css/variable.less";
  @import "~common/css/mixin.less";

  .rank {
    position: fixed;
    top: 88px;
    left: 0;
    width: 100%;
    bottom: 0;
    .toplist {
      height: 100%;
      overflow: hidden;
      .item {
        display: flex;
        margin: 0 20px;
        padding-top: 20px;
        height: 100px;
        &:last-child {
          padding-bottom: 20px;
        }
        .icon {
          flex: 0 0 100px;
          width: 100px;
          height: 100px;
        }
        .songlist {
          flex: 1;
          display: flex;
          flex-direction: column;
          justify-content: center;
          padding: 0 20px;
          height: 100px;
          overflow: hidden;
          font-size: @font-size-small;
          color: @color-text-d;
          background: @color-highlight-background;
          .song {
            .no-wrap();
            line-height: 26px;
          }
        }
      }
    }
    .loading-wrap {
      position: absolute;
      width: 100%;
      top: 50%;
      transform: translateY(-50%);
    }
  }
</style>
